package com.llvision.ndkdemoprocessdemo;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * todo
 *
 * @author liuhui
 * @date 2019/1/7
 */
public class QueueTest {
    static ArrayBlockingQueue<Long> QUEUE = new ArrayBlockingQueue<>(20);
    static long i = 0;
    static boolean isStop = false;
    public static void main(String[] args) {
        Thread thread1 = new Thread(){
            @Override
            public void run() {
                while (!isStop) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    QUEUE.offer(++i);
                }
            }
        };


        Thread thread2 = new Thread(){
            boolean isStopped = false;
            @Override
            public void run() {
                while (!isStopped){
                    try {
                        System.out.println("thread2--->"+QUEUE.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        isStopped = true;
                    }
                }
            }
        };


        thread1.start();
        thread2.start();
        try {
            Thread.sleep(5000);
            isStop = true;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (thread2 != null && !thread2.isInterrupted()) {
            thread2.interrupt();
            thread2 = null;
        }
    }
}
